AI Lessons
Lesson 10: Detecting faces in real time, Detecting multiple faces

Purpose: To use a pre-trained model to find faces in a live webcam picture, and to highlight the landmark points of the detected faces.

No. of Classes

1 - (Time : 1 hour 30 minutes, Laptops/desktops : 10, Students strength : 15 to 20).

Materials Required

Laptop / Desktop with Internet connections / Wi-Fi, and a webcam on every machine.

Prior knowledge
  • Browsers
  • Basics of JavaScript - variables, functions and arrays
  • Drawing shapes on a p5.js canvas
  • Using a pre-trained model on a live webcam feed
Exercises

Exercise (1)



Exercise (2)



  • Extend the program to detect and track multiple faces at the same time.

Solutions



Teacher's Instruction:
  1. Check that the webcam works before the class. ml5.js loads the model over the internet, so working internet connection is required
  2. Start from the base code linked in Exercise 1 rather than typing the webcam setup again.
  3. Explain that the face model works the same way as the hand model from the earlier lessons - it is pre-trained, and returns coordinates for the landmark points on the detected face in the webcam feed. The model is trained to find faces, but it does not know whose face it is.
  4. Point out what the model gives back for each face: an array of landmark points, given as x and y coordinates. Drawing the points is just using those coordinates.
  5. The results arrive as an array with one entry per face, so detecting several faces is the same code running once for each entry. This is the same array idea used for multiple hands.
  6. Try it deliberately with a face turned sideways, partly covered, or in poor light, so the students see where detection fails rather than only where it works.
  7. Discuss where face detection is already used, and raise consent - a camera that finds faces is not the same as one that recognises who they are. That difference is covered again in later lessons.
  8. Points to Ponder:
    • Cameras that spot faces are already in phones, shops and stations. When does that help you, and when would you want to be able to say no?
    • Try to point a photo from your phone camera at your webcame and check if it will detect the face from that photo. Why can't the model tell the difference between a live face and a photo?